package me.xiaosiqi.server_info.tool;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;

import java.io.*;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.*;

public class IpTool {
    static final String tmpdir = System.getProperty("java.io.tmpdir") + "/server-status-information";
    public static boolean CheckIpChange(String ipInfo) {
        File tmpdirFile = new File(tmpdir);
        if (!tmpdirFile.isDirectory()) {
            tmpdirFile.mkdirs();
        }
        BufferedReader bufferedReader = null;
        try {
            File file = new File(tmpdirFile, "ipinfo.txt");
            if (!file.exists()){
                return false;
            }
            bufferedReader = new BufferedReader(new FileReader(new File(tmpdirFile, "ipinfo.txt")));
            String readLine = bufferedReader.readLine();
            if (StrUtil.equals(readLine, ipInfo)) {
                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null!= bufferedReader){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    public static boolean updataIpInfo(String ipInfo) {
        File tmpdirFile = new File(tmpdir);
        if (!tmpdirFile.isDirectory()) {
            tmpdirFile.mkdirs();
        }
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(new File(tmpdirFile, "ipinfo.txt")));
            bufferedWriter.write(ipInfo);
            bufferedWriter.flush();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != bufferedWriter) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

   public static Map<String, List<String>> getIpInfo(){
         Map<String, List<String>> ipInfo =new HashMap<>();
       try {
           Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
           Enumeration<InetAddress> addresses;
           while (en.hasMoreElements()) {
               NetworkInterface networkinterface = en.nextElement();
               addresses = networkinterface.getInetAddresses();
               List<String> ipList =new ArrayList<>();
               while (addresses.hasMoreElements()) {
                   String hostAddress = addresses.nextElement().getHostAddress();
                   if (hostAddress.startsWith("fe80")){
                       continue;
                   }
                   if (hostAddress.startsWith("127")){
                       continue;
                   }
                   if (hostAddress.startsWith("0:0:0:0:0:0:0:1")){
                       continue;
                   }
                   if (hostAddress.contains("%")){
                       ipList.add(hostAddress.split("%")[0]);
                   }else {
                       ipList.add(hostAddress);
                   }
               }
               if (CollectionUtil.isNotEmpty(ipList)){
                   ipInfo.put(networkinterface.getName(),ipList);
               }
           }
           return ipInfo;
       } catch (SocketException e) {
           e.printStackTrace();
       }
       return ipInfo;
    }
}
